package com.czy.mapper;

import com.czy.dto.AdminBorrowPageDto;
import com.czy.dto.UpdateAssetQuantityDto;
import com.czy.dto.UserBorrowPageDto;
import com.czy.entity.BorrowRecord;
import com.czy.vo.BorrowRecordInfoVo;
import com.czy.vo.BorrowRecordPageVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface BorrowRecordMapper {

    // 借出总资产数(实际归还时间为空的资产为借出资产数)
    @Select("select COALESCE(SUM(quantity), 0) FROM borrowrecord where actual_return_time is NULL")
    public Integer borrowRecordSum();

    // 获取最近借出的资产数据
    public List<BorrowRecordInfoVo> loanRecords();

    // 分页查询用户借用资产记录
    public List<BorrowRecordPageVo> userPage(UserBorrowPageDto userBorrowPageDto);

    // 实际归还的时间
    @Select("update borrowrecord set actual_return_time = #{now} where borrow_id =#{id}")
    public void updateReturnTime(Integer id, LocalDateTime now);

    // 归还后，资产增加数量
    public UpdateAssetQuantityDto addAssetNumById(Integer id);

    // 添加借用资产记录
    @Insert("insert into borrowrecord (asset_id, user_id, borrow_time, quantity , expected_return_time) " +
            "values (#{assetId} , #{userId} , #{borrowTime} ,#{quantity} , #{expectedReturnTime})")
    public void addBorrowRecord(BorrowRecord borrowRecord);

    // 管理员查询所有借用记录信息
    public List<BorrowRecordPageVo> adminPage(AdminBorrowPageDto adminBorrowPageDto);
}
